//
//  ViewController.swift
//  剂查查
//
//  Created by shoubin tao on 2024/11/30.
//  Copyright © 2024 shipin. All rights reserved.
//

import UIKit
import CommonTools
import CJMobileAd
import MBProgressHUD

class JCCScanViewController: UIViewController,CJBannerAdDelegate {

    var hasToConnect = false
    var bannerAd : CJBannerAd?
    var bannerView : UIView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.setNavigationBarHidden(true, animated: true)
        let qrView = QRScanView(widthFrame: view.bounds, scanDelegate: self)
        qrView.fullScreenScan = true
        view.addSubview(qrView)
        qrView.startQRScan()
        
        let backBtn = UIButton(image: UIImage(named: "white_back"), title: nil, titleColor: nil, font: nil, in: view) { make in
            make.leading.equalTo(16)
            make.top.equalTo(54)
            make.width.height.equalTo(24)
        }
        
        backBtn.addBlock(for: .touchUpInside) {[unowned self] _ in
            navigationController?.popViewController(animated: true)
        }
        
        let animateIv = UIImageView(image: nil, in: view) { make in
            make.center.equalToSuperview()
            make.width.equalTo(250)
            make.height.equalTo(325)
        }
        if let gifUrl = Bundle.main.url(forResource: "scan_animation", withExtension: "gif") {
            if let gifData = try? Data(contentsOf: gifUrl) {
                let image = UIImage.sd_image(withGIFData: gifData)
                animateIv.image = image
            }
        }
        
        self.bannerView = UIView(frame: CGRectMake(10, screenHeight() - 100, screenWidth() - 20, UIScreen.main.bounds.size.width*80/(screenWidth() - 20)))
        view.addSubview(bannerView)
        
        bannerAd = CJBannerAd(resourceId: "017fad6fe3160b9f", rootViewController: self, rect: CGRectMake(0, 0, screenWidth() - 20, UIScreen.main.bounds.size.width*80/(screenWidth() - 20)))
        bannerAd?.delegate = self
        bannerAd?.loadData()
       
    }
    
    func bannerAd(onShow bannerAd: Any) {
        print("onShow")
    }
    
    func bannerAd(onClicked bannerAd: Any) {
        print("onClicked")
    }
    
    func bannerAd(onClosed bannerAd: Any) {
        print("onClosed")
    }
    
    func bannerAdDidLoad(_ bannerView: UIView, resourceId: String) {
        self.bannerView.addSubview(bannerView)
    }
    
    func bannerAdLoadFailed(_ bannerAd: Any, error: any Error) {
        print("bannerAdLoadFailed\(error)")
    }
}

extension JCCScanViewController: QRScanViewDelegate {
    func qrScanMetadataObject(byScan metadataObj: AVMetadataMachineReadableCodeObject) {
        let scanResult = metadataObj.stringValue ?? ""
        if hasToConnect == false {
            XWLog(value: scanResult)
            hasToConnect = true
//            tool.configure(url: result)
            JCCNet.getScanningDetail(code:scanResult) { model, error in
                if (model?.isSuccess == true){
                    let dict = model?.result as! [String:Any]
                    if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "JCCScanResultViewController") {
                        var vc:JCCScanResultViewController = viewController as? JCCScanResultViewController ?? JCCScanResultViewController()
                        vc.resultDict = dict
                        self.navigationController?.pushViewController(viewController, animated: true)
                    }
                    else {
                        self.navigationController?.popViewController(animated: true)
                    }
                }
                
            }
            
        }
        
    }
    
    func qrScanNoAuthorizationGoSetting() {
        navigationController?.popViewController(animated: true)
    }
    
    func qrScanNoAuthorizationGoBack() {
        
    }
    
    
}
